package com.vhall.component.controller.praise;

import com.vhall.component.entity.praise.vo.RoomLikeReqVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.VssToken;
import com.vhall.component.plugin.common.support.VssTokenThreadLocal;
import com.vhall.component.service.praise.RoomLikeService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author lsl
 * @since 2021/6/15
 */
@VhallComponent
public class RoomLikeController implements RoomLikeFacade {


    @Autowired
    private RoomLikeService roomLikeService;

    @Override
    public Long countIsLike(String accountId, String roomId) {
        return roomLikeService.countIsLike(accountId, roomId);
    }

    @Override
    @VssTokenAuth
    public Boolean like(RoomLikeReqVO reqVO) {

        VssToken token = checkVssToken();
        Integer num = reqVO.getNum() > 500 ? 500 : reqVO.getNum();
        Integer type = reqVO.getType() == null ? 0 : reqVO.getType();
        reqVO.setNum(num);
        reqVO.setType(type);
        roomLikeService.like(reqVO, token);
        return true;
    }

    @Override
    @VssTokenAuth
    public Boolean forward(RoomLikeReqVO reqVO) {
        VssToken token = checkVssToken();
        roomLikeService.forward(reqVO, token);
        return true;
    }

    private VssToken checkVssToken() {
        VssToken token = VssTokenThreadLocal.getToken();
        if (token == null) {
            throw new BusinessException(BizErrorCode.AUTH_LOGIN_NEEDS);
        }
        return token;
    }
}
